package com.jumi.microservice.mq;

import com.aliyun.openservices.ons.api.MessageListener;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.aliyun.openservices.ons.api.bean.ConsumerBean;
import com.aliyun.openservices.ons.api.bean.Subscription;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

@Configuration
public class ConsumerClient {

    @Autowired
    private RocketMqConfig mqConfig;

    @Resource
    OrderAfterSaleListener orderAfterSaleListener;
    @Resource
    OrderAccomplishListener orderAccomplishListener;

    @Bean(initMethod = "start", destroyMethod = "shutdown")
    public ConsumerBean buildOrderConsumer() {
        ConsumerBean orderConsumerBean = new ConsumerBean();
        //配置文件
        Properties properties = mqConfig.getMqProperties();
        properties.setProperty(PropertyKeyConst.GROUP_ID, mqConfig.getGroup());
        orderConsumerBean.setProperties(properties);
        //订阅orderAccomplish
        Map<Subscription, MessageListener> subscriptionTable = new HashMap<Subscription, MessageListener>();
        Subscription subscription = new Subscription();
        subscription.setTopic("orderAccomplish");
        subscriptionTable.put(subscription, orderAccomplishListener);
        //订阅orderAfterSale
        subscription = new Subscription();
        subscription.setTopic("orderAfterSale");
        subscriptionTable.put(subscription, orderAfterSaleListener);
        //订阅多个topic如上面设置
        orderConsumerBean.setSubscriptionTable(subscriptionTable);
        return orderConsumerBean;
    }

}
